	.module spi.c
	.area text(rom, con, rel)
	.dbfile D:\01.Projects\JKElectronics\ATMEGA\CoreModule\sw\examples\lib\spi.c
	.dbfunc e bsp_spi_gpio_init _bsp_spi_gpio_init fV
	.even
_bsp_spi_gpio_init::
	.dbline -1
	.dbline 7
; 
; 
; #include "hw_config.h"
; #include "spi.h"
; 
; void bsp_spi_gpio_init() 
; {
	.dbline 10
; 	// setup I/O ports 
; 
; 	SPI_PORT &= ~((1 << SPI_SCK_PIN_NO) | (1 << SPI_MOSI_PIN_NO));	// low bits
	in R24,0x18
	andi R24,249
	out 0x18,R24
	.dbline 11
; 	SPI_PORT |= (1 << SPI_MISO_PIN_NO);						// high bits
	sbi 0x18,3
	.dbline 12
; 	SPI_DDR  |= (1<<SPI_SCK_PIN_NO) | (1<<SPI_MOSI_PIN_NO);	// direction
	in R24,0x17
	ori R24,6
	out 0x17,R24
	.dbline 14
; 
; 	SPI_PORT |= (1 << SPI_CS_PIN_NO);	// Initial level is high	
	sbi 0x18,0
	.dbline 15
; 	SPI_DDR  |= (1 << SPI_CS_PIN_NO);	// Direction is output	
	sbi 0x17,0
	.dbline -2
L4:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e bsp_spi_init _bsp_spi_init fV
	.even
_bsp_spi_init::
	.dbline -1
	.dbline 64
; }
; 
; #if 0
; void bsp_spi_init() 
; {
; 	// also need to set SS as output
; 	SPI_DDR |= (1<<SPI_CS_PIN_NO);
; 
; 	SPCR = (1<<MSTR)|(1<<SPE);	// enable SPI interface
; 	SPSR = (1<<SPI2X);  // SPI Double speed
; }
; 
; 
; // SPI speed setting function
; void bsp_set_spi_speed (spi_speed_define_type speed)
; {
; 	if( speed == SPI_SPEED_HIGH )
; 	{
; 		SPCR =  _BV(SPE)|_BV(MSTR);
; 		SPCR &= ~_BV(SPR1);	
; 		SPCR &= ~_BV(SPR0);		
; 		SPSR |= _BV(SPI2X);	
; 	}
; 	else
; 	{
; 		if( F_CPU == 16000000UL )
; 		{
; 			SPCR =   _BV(SPE)|_BV(MSTR)|_BV(SPR1)|_BV(SPR0);
; 			SPSR &= ~_BV(SPI2X);	
; 		}
; 		else if( F_CPU == 7372800UL )
; 		{
; 			SPCR =   _BV(SPE)|_BV(MSTR)|_BV(SPR1);
; 			SPCR &= ~_BV(SPR0);		
; 			SPSR &= ~_BV(SPI2X);	
; 		
; 		}
; 		else
; 		{
; 			SPCR =   _BV(SPE)|_BV(MSTR)|_BV(SPR1)|_BV(SPR0);
; 			SPSR &= ~_BV(SPI2X);	
; 		
; 		}		
; 	}
; }
; #endif
; 
; void bsp_spi_init() 
; {
	.dbline 66
; 	// also need to set SS as output
; 	SPI_DDR |= (1<<SPI_CS_PIN_NO);
	sbi 0x17,0
	.dbline 68
; 
; 	SPCR = (1<<MSTR)|(1<<SPE);	// enable SPI interface
	ldi R24,80
	out 0xd,R24
	.dbline 69
; 	SPSR = (1<<SPI2X);  // SPI Double speed
	ldi R24,1
	out 0xe,R24
	.dbline -2
L5:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e bsp_set_spi_speed _bsp_set_spi_speed fV
;          speed -> R16
	.even
_bsp_set_spi_speed::
	.dbline -1
	.dbline 74
; }
; 
; // SPI speed setting function
; void bsp_set_spi_speed (spi_speed_define_type speed)
; {
	.dbline 75
; 	if( speed == SPI_SPEED_HIGH )
	tst R16
	brne L7
X0:
	.dbline 76
; 	{
	.dbline 77
; 		SPCR =  _BV(SPE)|_BV(MSTR);
	ldi R24,80
	out 0xd,R24
	.dbline 78
; 		SPSR |= _BV(SPI2X);	
	sbi 0xe,0
	.dbline 79
; 	}
	xjmp L8
L7:
	.dbline 81
; 	else
; 	{
	.dbline 82
; 		SPCR =   _BV(SPE)|_BV(MSTR)|_BV(SPR1)|_BV(SPR0);
	ldi R24,83
	out 0xd,R24
	.dbline 83
; 		SPSR &= ~_BV(SPI2X);	
	cbi 0xe,0
	.dbline 84
; 	}
L8:
	.dbline -2
L6:
	.dbline 0 ; func end
	ret
	.dbsym r speed 16 c
	.dbend
	.dbfunc e bsp_readwritebyte_spi _bsp_readwritebyte_spi fc
;        tx_data -> R16
	.even
_bsp_readwritebyte_spi::
	.dbline -1
	.dbline 88
; }
; 
; u8 bsp_readwritebyte_spi (u8 tx_data)
; {
	.dbline 89
; 	SPDR = tx_data;
	out 0xf,R16
L10:
	.dbline 91
; 	while(!(SPSR & (1<<SPIF)))
; 	{}
	.dbline 91
L11:
	.dbline 90
	sbis 0xe,7
	rjmp L10
X1:
	.dbline 92
; 	return SPDR;
	in R16,0xf
	.dbline -2
L9:
	.dbline 0 ; func end
	ret
	.dbsym r tx_data 16 c
	.dbend
; }
; 
